package com.woniu.adopt_web.filter;

import com.woniu.adopt_web.common.Constant;
import com.woniu.adopt_web.service.UserService;
import com.woniu.adopt_web.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFilter implements Filter, Constant {

    //    @Autowired
    private UserService userService;

    /**
     * filter中无法使用autowired
     *
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(filterConfig.getServletContext());
        UserService userService = context.getBean(UserService.class);
        this.userService = userService;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            String content = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("auto")) {
                    content = cookie.getValue();
                }
            }
            if (content != null) {
                String[] split = content.split(":");
                String username = split[0];
                String password = split[1];

                UserVo login = userService.login(username, password);
                if (login != null) {
                    login.setPic(ADOPT_IMG_URL + login.getPic());
                    //可以自动登录
                    HttpSession session = request.getSession();
                    session.setAttribute("loginUser", login);
                    //查询未读消息数量
                    int result=userService.findUnRead(login.getId());
                    session.setAttribute("msgNum", result);
                    HttpServletResponse response = (HttpServletResponse) servletResponse;
                    //回到上一页并刷新
                    PrintWriter out = response.getWriter();
                    out.write("<script>window.location.href = 'index.html'; </script>");
                    out.flush();
                    out.close();
                } else {
                    filterChain.doFilter(servletRequest, servletResponse);
                }
//                if (login != null && login.getPassword().equals(password)) {
//                if(username!=null && password!=null){//暂时的 普通查询太慢 换redis
                //---------
//                    UserVo login = new UserVo();
//                    login.setUsername(username);
//                    login.setPassword(password);
//                    login.setPic(ADOPT_IMG_URL+login.getPic());
//                    login.setPic(ADOPT_IMG_URL+"default.jpg");
                //----------

            } else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        } else {
            //本地没有时 直接放行
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
